/**********************************************************
 * GeneralOptionsAction.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 10:47:42 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;

import com.printeverywhere.delegator.CurrencyServiceDelegator;
import com.printeverywhere.delegator.GeneralOptionsServiceDelegator;
import com.printeverywhere.delegator.ListingOptionsServiceDelegator;
import com.printeverywhere.dto.Currency;
import com.printeverywhere.dto.DateFormats;
import com.printeverywhere.dto.GeneralOptions;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.DateUtil;

// TODO: Auto-generated Javadoc
/**
 * The Class GeneralOptionsAction.
 */
public class GeneralOptionsAction  extends AdminSystemSetup {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(GeneralOptionsAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	private GeneralOptions generalOptions;
	private List<Currency> currencyList = new ArrayList<Currency>();
	private List<DateFormats> dates = new ArrayList<DateFormats>();

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		try {
			getGeneralOptionsData();
		} catch (Exception e) {
			return INPUT;
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String updateGeneralOptions() {
		try {
			GeneralOptionsServiceDelegator.updateGeneralOptions(generalOptions);
			this.generalOptions = GeneralOptionsServiceDelegator
			.getGeneralOptions();
		} catch (HibernateException e) {
			getGeneralOptionsData();
			addActionError("Input valid Data.");
			logger.error(AdminConstants.ERROR_IN_UPDATING_THE_RECORD
					+ e.getMessage());
			return INPUT;
		}
		return SUCCESS;
	}

	public void getGeneralOptionsData(){
		try{
			if(dates.size() > 0){
				dates.clear();
			}
			if(currencyList.size() > 0){
				currencyList.clear();
			}
			dates = DateUtil.getFormattedDates();
			this.generalOptions = GeneralOptionsServiceDelegator
			.getGeneralOptions();
			currencyList = CurrencyServiceDelegator.getList();
		}catch (Exception e) {
			addActionError("Error while retrieving data for general options.");
			logger.error(e.toString());
		}
	}
	@Override
	public void validate() {
		try{
			dates = DateUtil.getFormattedDates();
			currencyList = CurrencyServiceDelegator.getList();
			if((generalOptions != null)&&(generalOptions.getMaximumAllowedNegativeBalance() != null)){
				if(generalOptions.getMaximumAllowedNegativeBalance() >= 0){
					addActionError("Only negative values allowed for Maximum Allowed Negative Balance.");
					getGeneralOptionsData();
				}
			}
			if(super.hasFieldErrors()){
				getGeneralOptionsData();
			}
		}catch (Exception e) {
		}
	}

	public GeneralOptions getGeneralOptions() {
		return generalOptions;
	}

	public void setGeneralOptions(GeneralOptions generalOptions) {
		this.generalOptions = generalOptions;
	}

	public List<DateFormats> getDates() {
		return dates;
	}

	public void setDates(List<DateFormats> dates) {
		this.dates = dates;
	}

	public List<Currency> getCurrencyList() {
		return currencyList;
	}

	public void setCurrencyList(List<Currency> currencyList) {
		this.currencyList = currencyList;
	}
}